package com.tepth.maintenancesystem.view;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.superrecycleview.superlibrary.adapter.SuperBaseAdapter;
import com.superrecycleview.superlibrary.recycleview.ProgressStyle;
import com.superrecycleview.superlibrary.recycleview.SuperRecyclerView;
import com.tepth.maintenancesystem.R;
import com.tepth.maintenancesystem.bean.VehicleObj;
import com.tepth.maintenancesystem.presenter.MyDataAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends AppCompatActivity {

//    private SubscriberOnNextListener getTopMovieOnNext;

    @BindView(R.id.rl_barcode)
    RelativeLayout rl_barcode;
    @BindView(R.id.rl_myInformation)
    RelativeLayout rl_myInformation;

    private List<VehicleObj> datas = new ArrayList<>();
    private Context context;
    private SuperRecyclerView main_list;
    private MyDataAdapter myAdapter;
    private View headerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        context=this;
        addData();
        initView();
        initAdapter();
    }

    private void initView() {
        main_list = (SuperRecyclerView) findViewById(R.id.main_list);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        main_list.setLayoutManager(layoutManager);
        main_list.setRefreshEnabled(true);//可以定制是否开启下拉刷新
        main_list.setLoadMoreEnabled(true);//可以定制是否开启加载更多
        main_list.setLoadingListener(new PullToRefreshListener());//下拉刷新，上拉加载的监听
        main_list.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);//下拉刷新的样式
        main_list.setLoadingMoreProgressStyle(ProgressStyle.BallClipRotate);//上拉加载的样式
        main_list.setArrowImageView(R.drawable.arrow_down);//设置下拉箭头
        headerView = getLayoutInflater().inflate(R.layout.vehicle_list_head, (ViewGroup) main_list.getParent(), false);
    }

    private void initAdapter() {
        myAdapter = new MyDataAdapter(context,datas);
        myAdapter.addHeaderView(headerView);
        main_list.setAdapter(myAdapter);
        myAdapter.setOnItemClickListener(new SuperBaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, Object item, int position) {
                Intent intent=new Intent(context,VehicleDetailsActivity.class);
                startActivity(intent);
            }
        });
    }

    private class PullToRefreshListener implements SuperRecyclerView.LoadingListener{

        @Override
        public void onLoadMore() {
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    addData();
                    main_list.completeLoadMore();
                }
            }, 3000);
        }

        @Override
        public void onRefresh() {
//            if(dataList.size() >= 50){
//                superRecyclerView.setNoMore(true);
//                return;
//            }
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    newData();
                    main_list.completeRefresh();
                    myAdapter.notifyDataSetChanged();
                }
            }, 3000);
        }
    }

//    @OnClick(R.id.click_me_BN)
//    public void onClick() {
//        getMovie();
//    }

    //进行网络请求
    private void getMovie() {
//        String baseUrl = "https://api.douban.com/v2/movie/";
//        Retrofit retrofit = new Retrofit.Builder()
//                .baseUrl(baseUrl)
//                .addConverterFactory(GsonConverterFactory.create())
//                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
//                .build();
//        MovieService movieService = retrofit.create(MovieService.class);
//        Call<MovieEntity> call = movieService.getTopMovie(0, 10);//用传统的Retrofit请求网络
//        call.enqueue(new Callback<MovieEntity>() {
//            @Override
//            public void onResponse(Call<MovieEntity> call, Response<MovieEntity> response) {
//                resultTV.setText(response.body().toString());
//            }
//
//            @Override
//            public void onFailure(Call<MovieEntity> call, Throwable t) {
//                resultTV.setText(t.getMessage());
//            }
//        });
//        movieService.getTopMovie(0, 10).//用RxJava+Retrofit请求网络
//                subscribeOn(Schedulers.io()).
//                observeOn(AndroidSchedulers.mainThread()).
//                subscribe(new Subscriber<MovieEntity>() {
//                    @Override
//                    public void onCompleted() {
//                        Toast.makeText(MainActivity.this, "Get Top Movie Completed", Toast.LENGTH_SHORT).show();
//                    }
//
//                    @Override
//                    public void onError(Throwable e) {
//                        resultTV.setText(e.getMessage());
//                    }
//
//                    @Override
//                    public void onNext(MovieEntity movieEntity) {
//                        resultTV.setText(movieEntity.toString());
//                    }
//                });
//        Subscriber<List<Subject>> subscriber=new Subscriber<List<Subject>>() {//用对象封装了返回的结果并显示出来
//            @Override
//            public void onCompleted() {
//                Toast.makeText(MainActivity.this, "Get Top Movie Completed", Toast.LENGTH_SHORT).show();
//            }
//
//            @Override
//            public void onError(Throwable e) {
//                resultTV.setText(e.getMessage());
//            }
//
//            @Override
//            public void onNext(List<Subject> subjects) {
//                StringBuffer sb=new StringBuffer();
//                for (int i=0;i<subjects.size();i++){
//                    sb.append(subjects.get(i).toString());
//                }
//                resultTV.setText(sb);
//            }
//        };
//        HttpMethods.getInstance().getTopMovie(subscriber,0,10);

//        HttpMethods.getInstance().getTopMovie(
//                new ProgressSubscriber(getTopMovieOnNext, MainActivity.this),
//                0, 10);
    }

    /**
     * 添加数据
     */
    private void addData() {
        datas.add(new VehicleObj("81213","混合动力  138","2016/11/28 10:08","1"));
        datas.add(new VehicleObj("81212","混合动力  138","2016/11/28 10:08","1"));
        datas.add(new VehicleObj("81211","混合动力  140","2016/11/28 10:08","2"));
        datas.add(new VehicleObj("81210","混合动力  140","2016/11/28 10:08","3"));
    }

    public void newData() {
        datas.clear();
        addData();
    }

}
